<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="课文名称" prop="textName">
        <el-input
          v-model="queryParams.textName"
          placeholder="请输入课文名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="作者" prop="textAuthor">
        <el-input
          v-model="queryParams.textAuthor"
          placeholder="请输入作者"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年级id" prop="gradeId">
        <el-input
          v-model="queryParams.gradeId"
          placeholder="请输入年级id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单元id" prop="unitId">
        <el-input
          v-model="queryParams.unitId"
          placeholder="请输入单元id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名师" prop="famousTeacherId">
        <el-input
          v-model="queryParams.famousTeacherId"
          placeholder="请输入名师"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名师" prop="teacherName">
        <el-input
          v-model="queryParams.teacherName"
          placeholder="请输入名师"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="音频文件" prop="ftextIle">
        <el-input
          v-model="queryParams.ftextIle"
          placeholder="请输入音频文件"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文本内容id" prop="contentId">
        <el-input
          v-model="queryParams.contentId"
          placeholder="请输入文本内容id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学期:" prop="semester">
        <el-input
          v-model="queryParams.semester"
          placeholder="请输入学期："
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="createdBy">
        <el-input
          v-model="queryParams.createdBy"
          placeholder="请输入创建人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="删除：0-否，1-是" prop="deleteFlag">
        <el-input
          v-model="queryParams.deleteFlag"
          placeholder="请输入删除：0-否，1-是"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createdTime">
        <el-date-picker
          clearable
          size="small"
          v-model="queryParams.createdTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择创建时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="更新人" prop="updatedBy">
        <el-input
          v-model="queryParams.updatedBy"
          placeholder="请输入更新人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="更新时间" prop="updatedTime">
        <el-date-picker
          clearable
          size="small"
          v-model="queryParams.updatedTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择更新时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <!-- 新增 修改 删除 导出 4个按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['recitation:text:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['recitation:text:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['recitation:text:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['recitation:text:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="textList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="课文id" align="center" prop="textId" />
      <el-table-column label="课文名称" align="center" prop="textName" />
      <el-table-column label="作者" align="center" prop="textAuthor" />
      <el-table-column label="年级id" align="center" prop="gradeId" />
      <el-table-column label="单元id" align="center" prop="unitId" />
      <el-table-column label="名师" align="center" prop="famousTeacherId" />
      <el-table-column label="名师" align="center" prop="teacherName" />
      <el-table-column label="音频文件" align="center" prop="ftextIle" />
      <el-table-column label="文本内容id" align="center" prop="contentId" />
      <el-table-column
        label="学期：0：上；1：下"
        align="center"
        prop="semester"
      />
      <el-table-column label="创建人" align="center" prop="createdBy" />
      <el-table-column
        label="删除：0-否，1-是"
        align="center"
        prop="deleteFlag"
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createdTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新人" align="center" prop="updatedBy" />
      <el-table-column
        label="更新时间"
        align="center"
        prop="updatedTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatedTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['recitation:text:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['recitation:text:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改课文对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item
              label="课文名称"
              prop="textName"
              class="grid-content bg-purple"
            >
              <el-input
                v-model="form.textName"
                placeholder="请输入"
              /> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item
              label="归属年级"
              prop="gradeId"
              class="grid-content bg-purple-light"
            >
              <el-select
                v-model="form.gradeId"
                placeholder="请选择"
              ></el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item
              label="语文作者:"
              prop="textAuthor"
              class="grid-content bg-purple"
            >
              <el-input v-model="form.textAuthor" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="归属学期:"
              prop="semester"
              class="grid-content bg-purple-light"
            >
              <el-select
                v-model="form.semester"
                placeholder="请选择"
              ></el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item
              label="名嘴姓名:"
              prop="famousTeacherId"
              class="grid-content bg-purple"
            >
              <el-select
                v-model="form.famousTeacherId"
                placeholder="请输入"
              ></el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="归属单元:"
              prop="unitId"
              class="grid-content bg-purple-light"
            >
              <el-select v-model="form.unitId" placeholder="请选择"></el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item
              label="课文全文朗读文本:"
              label-width="140px"
              prop="contentId"
            >
              <el-input
                type="textarea"
                v-model="form.contentId"
                rows="8"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- 课文全文朗读音频 -->
        <el-row>
          <el-col :span="8">
            <el-form-item
              label="课文全文朗读音频:"
              label-width="140px"
              prop="contentId"
            >
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-input type="text" />
          </el-col>
          <!--  :action="upLoadAudio"   :file-list="fileList"-->
          <el-upload
          action="#"
            id="audioUpload"
            class="avatar-uploader"
            :show-file-list="true"
            :multiple="false"
            :on-remove="handleRemove"
            :limit="1"
            :auto-upload="true"
            :on-change="handleChangeAudio"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <el-button
              slot="trigger"
              size="small"
              type="primary"
              style="margin-left: 10px"
              >选取文件</el-button
            >
          </el-upload>
        </el-row>
        <!--  prop="contentId"待写因为跟上面的课文全文一样的props -->
        <el-row>
          <el-col :span="24">
            <el-form-item
              label="课文分段朗读第一段文本:"
              label-width="180px"
              prop="contentId"
            >
              <el-input
                type="textarea"
                v-model="form.contentId"
                rows="6"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- 课文分段朗读第一段音频 -->
        <el-row>
          <el-col :span="10">
            <el-form-item
              label="课文分段朗读第一段音频:"
              label-width="180px"
              prop="contentId"
            >
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-input type="text" style="margin-left：15px" />
          </el-col>
          <!--   :action="upLoadAudio"  :file-list="fileList" -->
          <el-col :span="4">
            <el-upload
            action="#"
              id="audioUpload"
              class="avatar-uploader"
              :show-file-list="true"
              :multiple="false"
              :on-remove="handleRemove"
              :limit="1"
              :auto-upload="true"
              :on-change="handleChangeAudio"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <el-button
                slot="trigger"
                size="small"
                type="primary"
                style="margin-left: 10px"
                >选取文件</el-button
              >
            </el-upload>
          </el-col>
        </el-row>
        <!-- 添加段落 -->
        <el-form-item class="addCard">
          <!--  -->
          <el-upload  action="#" list-type="picture-card" :auto-upload="false" >
            <i slot="default" class="el-icon-plus"  >添加段落</i>
            <div slot="file" slot-scope="{ file }" class="el-upload--picture-card">
              <img
                class="el-upload-list__item-thumbnail"
                :src="file.url"
                alt=""
              />
              <span class="el-upload-list__item-actions">
                <span
                  class="el-upload-list__item-preview"
                  @click="handlePictureCardPreview(file)"
                >
                  <i class="el-icon-zoom-in" style="width:200px"></i>
                </span>
                <span
                  v-if="!disabled"
                  class="el-upload-list__item-delete"
                  @click="handleDownload(file)"
                >
                  <i class="el-icon-download"></i>
                </span>
                <span
                  v-if="!disabled"
                  class="el-upload-list__item-delete"
                  @click="handleRemove(file)"
                >
                  <i class="el-icon-delete"></i>
                </span>
              </span>
            </div>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="" />
          </el-dialog>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listText,
  getText,
  delText,
  addText,
  updateText,
} from "@/api/recitation/text";

export default {
  name: "Text",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 课文表格数据
      textList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        textName: null,
        textAuthor: null,
        gradeId: null,
        unitId: null,
        famousTeacherId: null,
        teacherName: null,
        ftextIle: null,
        contentId: null,
        semester: null,
        createdBy: null,
        deleteFlag: null,
        createdTime: null,
        updatedBy: null,
        updatedTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deleteFlag: [
          {
            required: true,
            message: "删除：0-否，1-是不能为空",
            trigger: "blur",
          },
        ],
        createdTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" },
        ],
      },
      //
      dialogImageUrl: "",
      dialogVisible: false,
      disabled: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询课文列表 */
    getList() {
      this.loading = true;
      listText(this.queryParams).then((response) => {
        this.textList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        textId: null,
        textName: null,
        textAuthor: null,
        gradeId: null,
        unitId: null,
        famousTeacherId: null,
        teacherName: null,
        ftextIle: null,
        contentId: null,
        semester: null,
        createdBy: null,
        deleteFlag: null,
        createdTime: null,
        updatedBy: null,
        updatedTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.textId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加课文";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const textId = row.textId || this.ids;
      getText(textId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改课文";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.textId != null) {
            updateText(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addText(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const textIds = row.textId || this.ids;
      this.$confirm(
        '是否确认删除课文编号为"' + textIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delText(textIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "recitation/text/export",
        {
          ...this.queryParams,
        },
        `recitation_text.xlsx`
      );
    },
    // 增加页面 音频
    beforeAvatarUpload(file) {
      // 文件类型进行判断
      const isAudio = file.type === "audio/mp3" || file.type === "audio/mpeg";
      // 限制上传文件大小 2M
      const isLt2M = file.size / 1024 / 1024 < 2;
      const isTime60S = this.audioDuration >= 60 ? true : "";
      // 获取时长
      this.getTimes(file);
      if (!isAudio) {
        this.$message.error("上传文件只能是Mp3格式!");
        this.fileList = [];
      } else {
        if (!isLt2M) {
          this.$message.error("上传文件大小不能超过 2MB!");
          this.fileList = [];
        } else {
          if (!isTime60S) {
            this.$message.error("上传文件时长不能超过60秒!");
            this.fileList = [];
          }
        }
      }
      return isAudio && isLt2M && isTime60S;
    },
    getTimes(file) {
      var content = file;
      //获取录音时长
      var url = URL.createObjectURL(content);
      //经测试，发现audio也可获取视频的时长
      var audioElement = new Audio(url);
      audioElement.addEventListener("loadedmetadata", (_event) => {
        this.audioDuration = parseInt(audioElement.duration);
        // console.log(this.audioDuration);
      });
    },
    handleRemove(file) {
      console.log(file);
    },
    handlePictureCardPreview(file) {
      // this.dialogImageUrl = file.url;
      // this.dialogVisible = true;
    },
    handleDownload(file) {
      console.log(file);
    },
    handleAvatarSuccess() {},
    handleChangeAudio() {},
    //添加段落相关
    handleRemove(file) {
      console.log(file);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleDownload(file) {
      console.log(file);
    },
  },
};
</script>
<style >
  
/* .el-upload .el-upload--picture-card{
    border: 5px dashed #c0ccda;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    width: 375px;
    height: 100px;
    cursor: pointer;
    line-height: 100px;
} */

</style>